<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN""http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
  <head>
    <title>SRFI 78: Lightweight testing</title>
  </head>

  <body>

<H1>Title</H1>

Lightweight testing

<H1>Author</H1>

Sebastian Egner

<H1>Status</H1>

This SRFI is currently in ``final'' status.  To see an explanation of
each status that a SRFI can hold, see <A
HREF="http://srfi.schemers.org/srfi-process.html">here</A>.

To provide input on this SRFI, please <code><A
HREF="mailto:srfi-78@srfi.schemers.org">mail to
&lt;srfi-78@srfi.schemers.org&gt;</A></code>.  See <A
HREF=../srfi-list-subscribe.html>instructions here</A> to subscribe to
the list.  You can access previous messages via <A
HREF=mail-archive/maillist.html>the archive of the mailing list</A>.


<P><UL>
<LI>Received: 2005/10/25</LI>
<LI>Draft: 2005/11/11 - 2006/01/10</LI>
<LI>Draft extended: 2006/01/17 - 2006/01/31</LI>
<LI>Revised: 2006/01/18</LI>
<LI>Revised: 2006/02/08</LI>
<LI>Final: 2006/03/06</LI>
</UL>

<H1>Abstract</H1>

A simple mechanism is defined for testing Scheme programs.
As a most primitive example, the expression

<PRE>   (check (+ 1 1) => 3)
</PRE>

evaluates the expression <CODE>(+ 1 1)</CODE> and compares the result
with the expected result 3 provided after the syntactic
keyword <CODE>=&gt;</CODE>. Then the outcome of this comparison is reported
in human-readable form by printing a message of the form

<PRE>   (+ 1 1) => 2 ; *** failed ***
   ; expected result: 3
</PRE>

Moreover, the outcome of any executed check is recorded
in a global state counting the number of correct and failed
checks and storing the first failed check. At the end of a
file, or at any other point, the user can print a summary
using <CODE>check-report</CODE>.
<p>
In addition to the simple test above, it is also possible
to execute a parametric sequence of checks. Syntactically,
this takes the form of an eager comprehension in the sense
of <A HREF="http://srfi.schemers.org/srfi-42">SRFI 42</A> [5]. For example,

<PRE>   (check-ec (:range e 100)
             (:let x (expt 2.0 e)) 
             (= (+ x 1) x) => #f (e x))
</PRE>

This statement runs the variable <CODE>e</CODE> through {0..99} and
for each binding defines <CODE>x</CODE> as <CODE>(expt 2.0 e)</CODE>. Then it is 
checked if <CODE>(+ x 1)</CODE> is equal to <CODE>x</CODE>, and it is expected that
this is not the case (i.e. expected value is <CODE>#f</CODE>). The
trailing <CODE>(e x)</CODE> tells the reporting mechanism to print
the values of both <CODE>e</CODE> and <CODE>x</CODE> in case of a failed check.
The output could look like this:

<PRE>   (let ((e 53) (x 9007199254740992.0)) (= (+ x 1) x)) => #t ; *** failed ***
    ; expected result: #f
</PRE>

The specification of bindings to report, <CODE>(e x)</CODE> in the 
example, is optional but very informative.

Other features of this SRFI are:
<UL>
<LI>A way to specify a different equality predicate (default is <CODE>equal?</CODE>).</LI>
<LI>Controlling the amount of reporting being printed.</LI>
<LI>Switching off the execution and reporting of checks entriely.</LI>
<LI>Retrieving a boolean if all checks have been executed and passed.</LI>
</UL>

<H1>Rationale</H1>
The mechanism defined in this SRFI should be available in
every Scheme system because it has already proven useful
for interactive development---of SRFIs. 
<p>
Although it is extremely straight-forward, the origin of the
particular mechanism described here is the 'examples.scm' file
accompanying the reference implementation of SRFI 42 [5].
The same mechanism has been reimplemented for the reference
implementation of SRFI 67, and a simplified version is yet
again found in the reference implementation of SRFI 77.
<p>
The mechanism in this SRFI does not replace more sophisticated
approaches to unit testing, like SRFI 64 [1] or SchemeUnit [2].
These systems provide more control of the testing, separate
the definition of a test, its execution, and its reports, and
provide several other features.
<p>
Neil Van Dyke's Testeez library [3] is very close in spirit 
to this SRFI. In Testeez, tests are disabled by (re-)defining a
macro. The advantage of this method is that the code for the
test cases can be removed entirely, and hence even the dependency
on the Testeez library. This SRFI on the other hand, uses a
Scheme conditional (<CODE>COND</CODE>, <CODE>IF</CODE>) to prevent execution of the
testing code. This method is more dynamic but retains dead
testing code, unless a compiler and a module system are used
to apply constant folding and dead code elimination. The only
major addition in SRFI over Testeez is the comprehension for
formulating parametric tests.
<p>
Design considerations for this SRFI include the following:
<UL>
<LI> Reporting is human-readable and as specific as possible,
  i.e. not just "assertion failed" but the expression with
  actual and expected value, and if possibly the relevant 
  part of the bindings environment.</LI>
<LI> An effort is made to print closed Scheme expressions, i.e. 
  expressions that can directly be copy/pasted into a REPL
  for further analysis (e.g. the let expression in the abstract).</LI>
<LI> By default the checks report both correct and failed checks.
  However, it is possible to reduce the output---or even to 
  switch off the execution of checks. It has turned out useful
  to be able to run only some subset checks for the features
  currently under development. This can be done by changing
  the reporting mode between different sections.</LI>
<LI> The global state (correct/failed count) is not made available
  to the user program. This reduces the dependencies between
  different checks because it is not possible to use the state.</LI>
<LI> Ocassionally, it is useful to check that a certain expression 
  does <I>not</I> yield an ordinary result but raises an error. However, 
  R5RS [4] does not specify the mechanism by which this occurs
  (e.g. raising exception, breaking into a REPL, aborting the
  program, etc.). For this reason, this SRFI is restricted to 
  the case that the checked expressions evaluate normally.</LI>
<LI> Though usually I am very much in favor of strictly prefix
  syntax, for this SRFI I make an exception because the 
  infix "<CODE>=&gt;</CODE>" syntax is widely used and intuitive.</LI>
</UL>

<H1>Specification</H1>

<DL>
<DT><CODE>
(<a name="check">check</a> &lt;expr&gt; (=&gt; &lt;equal&gt;) &lt;expected&gt;)                                   </CODE>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MACRO<CODE><BR>
(check &lt;expr&gt;  =&gt;          &lt;expected&gt;)
</CODE></DT>
<DD>
   evaluates <CODE>&lt;expr&gt;</CODE> and compares the value to the value
   of <CODE>&lt;expected&gt;</CODE> using the predicate <CODE>&lt;equal&gt;</CODE>, which is
   <CODE>equal?</CODE> when omitted. Then a report is printed according
   to the current mode setting (see below) and the outcome
   is recorded in a global state to be used in <CODE>check-report</CODE>.
      The precise order of evaluation is that first <CODE>&lt;equal&gt;</CODE>
   and <CODE>&lt;expected&gt;</CODE> are evaluated (in unspecified order) and
   then <CODE>&lt;expr&gt;</CODE> is evaluated.<BR>
   Example: <CODE>(check (+ 1 1) =&gt; 2)</CODE>
</DD>
</DL>

<DL>
<DT><CODE>
(<a name="check-ec">check-ec</a> &lt;qualifier&gt;</CODE>*<CODE> &lt;expr&gt; (=&gt; &lt;equal&gt;) &lt;expected&gt; (&lt;argument&gt;</CODE>*<CODE>))   </CODE>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MACRO<CODE><BR>
(check-ec &lt;qualifier&gt;</CODE>*<CODE> &lt;expr&gt;  =&gt;          &lt;expected&gt; (&lt;argument&gt;</CODE>*<CODE>))<BR>
(check-ec &lt;qualifier&gt;</CODE>*<CODE> &lt;expr&gt; (=&gt; &lt;equal&gt;) &lt;expected&gt;)<BR>
(check-ec &lt;qualifier&gt;</CODE>*<CODE> &lt;expr&gt;  =&gt;          &lt;expected&gt;)
</CODE></DT>
<DD>
   an eager comprehension for executing a parametric sequence of checks.<p>
      Enumerates the sequence of bindings specified by <CODE>&lt;qualifier&gt;</CODE>*.
   For each binding evaluates <CODE>&lt;equal&gt;</CODE> and <CODE>&lt;expected&gt;</CODE> in unspecified
   order. Then evalues <CODE>&lt;expr&gt;</CODE> and compares the value obtained to the
   value of <CODE>&lt;expected&gt;</CODE> using the value of <CODE>&lt;equal&gt;</CODE> as predicate, which
   is <CODE>equal?</CODE> when omitted.
      The comprehension stops after the first failed check, if there
   is any. Then a report is printed according to the current mode 
   setting (see below) and the outcome is recorded in a global state 
   to be used in <CODE>check-report</CODE>. The entire <CODE>check-ec</CODE> counts as a single 
   check.<P>
      In case the check fails <CODE>&lt;argument&gt;</CODE>* is used for constructing an
   informative message with the argument values. Use <CODE>&lt;argument&gt;</CODE>* to
   list the relevant free variables of <CODE>&lt;expr&gt;</CODE> (see examples) that you
   want to have printed.<P>
      A <CODE>&lt;qualifier&gt;</CODE> is any qualifier of an eager comprehension as
   specified in <A HREF="http://srfi.schemers.org/srfi-42">SRFI 42</A> [5].<P>

   Examples:
   <PRE>     (check-ec (: e 100) (positive? (expt 2 e)) =&gt; #t (e)) ; fails on fixnums
     (check-ec (: e 100) (:let x (expt 2.0 e)) (= (+ x 1) x) =&gt; #f (x)) ; fails
     (check-ec (: x 10) (: y 10) (: z 10)
               (* x (+ y z)) =&gt; (+ (* x y) (* x z))
               (x y z)) ; passes with 10^3 cases checked
   </PRE>
</DD>
</DL>

<DL>
<DT><CODE>
(<a name="check-report">check-report</a>) </CODE>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PROCEDURE<CODE>
</CODE></DT>
<DD>
   prints a summary and the first failed check, if there is any,
   depending on the current mode settings.
</DD>
</DL>

<DL>
<DT><CODE>
(<a name="check-set-mode">check-set-mode!</a> mode) </CODE>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PROCEDURE<CODE>
</CODE></DT>
<DD>
   sets the current mode to mode, which must be a symbol in
   <CODE>'(off summary report-failed report)</CODE>, default is <CODE>'report</CODE>.
   Note that you can change the mode at any time, and that check,
   <CODE>check-ec</CODE> and <CODE>check-report</CODE> use the current value.
<P>
   The mode symbols have the following meaning:<BR>
     <CODE>off</CODE>:           do not execute any of the checks,<BR>
     <CODE>summary</CODE>:       print only summary in <CODE>(check-report)</CODE> and nothing else,<BR>
     <CODE>report-failed</CODE>: report failed checks when they happen, and in summary,<BR>
     <CODE>report</CODE>:        report every example executed.<BR>
</DD>
</DL>

<DL>
<DT><CODE>
(<a name="check-reset">check-reset!</a>) </CODE>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PROCEDURE<CODE>
</CODE></DT>
<DD>
   resets the global state (counters of correct/failed examples)
   to the state immediately after loading the module for the
   first time, i.e. no checks have been executed.
</DD>
</DL>

<DL>
<DT><CODE>
(<a name="check-passed">check-passed?</a> expected-total-count) </CODE>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PROCEDURE<CODE>
</CODE></DT>
<DD>
   <CODE>#t</CODE> if there were no failed checks and expected-total-count 
   correct checks, <CODE>#f</CODE> otherwise.<P>
     Rationale: This procedure can be used in automatized
   tests by terminating a test program with the statement
   <CODE>(exit (if (check-passed? </CODE><I>n</I><CODE>) 0 1))</CODE>.
</DD>
</DL>


<H1>Implementation</H1>

<A HREF="check.scm">check.scm</A>:
  implementation in R5RS + SRFI 23 (error) + SRFI 42 (comprehensions);
  tested under PLT 208p1 and Scheme 48 1.3.<br>

<A HREF="examples.scm">examples.scm</A>:
  a few examples.

<H1>References</H1>

<UL>
<LI><A NAME="1">[1]</A> 
    SRFI 64 by Per Bothner: A Scheme API for test suites. January 2005.
    <A HREF="http://srfi.schemers.org/srfi-64">http://srfi.schemers.org/srfi-64</A></LI>

<LI><A NAME="2">[2]</A> 
    Noel Welsh: SchemeUnit. February 2003.
    <A HREF="http://schematics.sourceforge.net/schemeunit.html">http://schematics.sourceforge.net/schemeunit.html</A></LI>

<LI><A NAME="3">[3]</A> 
    Neil Van Dyke: 
    Testeez, Lightweight Unit Test Mechanism for Scheme. May 2005.
    <A HREF="http://www.neilvandyke.org/testeez">http://www.neilvandyke.org/testeez</A></LI>

<LI><A NAME="4">[4]</A> 
    Revised^5 Report on the Algorithmic Language Scheme (R5RS).
    <A HREF="http://www.schemers.org/Documents/Standards/R5RS/">http://www.schemers.org/Documents/Standards/R5RS/</A></LI>

<LI><A NAME="5">[5]</A> 
    SRFI 42 by Sebastian Egner: Eager Comprehensions.
    <A HREF="http://srfi.schemers.org/srfi-42">http://srfi.schemers.org/srfi-42</A></LI>
</UL>

<H1>Copyright</H1>

Copyright (C) Sebastian Egner (2005-2006). All Rights Reserved.
<p>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 
<p>
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 
<p>
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
<p>
    <hr>
    <address>Author: <a href="mailto:srfi-78@sebastian-egner.net">srfi-78@sebastian-egner.net</a></address>
    <address>Editors: <a href="mailto:srfi-editors@srfi.schemers.org">srfi-editors@srfi.schemers.org</a></address>
  </body>
</html>
